home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / amos / jwindows.lha / Os&Xs.asc < prev    next >
Text File  |  1996-04-25  |  7KB  |  216 lines

  1. 'Note: 
  2. '   You may have trouble running this program from the editor. If so,
  3. 'change the Path$ variable in INITIALISE to point to the directory this
  4. 'example is in. This isn't a problem when compiled.
  5.  
  6. '**********************************************************************
  7. '* 
  8. '*   Os and Xs 
  9. '* 
  10. '**********************************************************************
  11.  
  12. '   As a break from all the useless little utilities I've been writing,
  13. 'here is a useless little game.
  14. '   It demonstrates the use of AMOS Pro drawing commands on intuition, and 
  15. 'more importantly, the use of the IDCMP_NEWSIZE flag. This flag allows 
  16. 'us to redraw the board in a new size every time the user changes the size 
  17. 'of the window. We also use some very simple menus.  
  18. '   And no, there's no fancy artificial intelligence. It's two player only.
  19.  
  20. '**********************************************************************
  21.  
  22. Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
  23. Global PATH$,OSVER
  24. Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
  25. Dim _OXGADS(0)
  26. Global _OXGADS()
  27. Global _OXMENU,_OXMENADD
  28. Global _OXWIND
  29.  
  30. On Error Proc _CLEANUP
  31.  
  32. '** _MOVE() contains various static strings for use as the window title
  33. '   _MYMOVE is 0 for Os to go, 1 for Xs
  34. Dim _MOVE(4)
  35. Global _MOVE(),_MYMOVE
  36. _MOVE(0)=J Make String("O's move")
  37. _MOVE(1)=J Make String("X's move")
  38. _MOVE(2)=J Make String("Os and Xs for AMOS")
  39. _MOVE(3)=J Make String("O wins!")
  40. _MOVE(4)=J Make String("X wins!")
  41.  
  42. '** _BOARD() represents the board, 0=blank, 1=O, 2=X 
  43. '   WIN holds the present winner 0=none, 1=O, 2=X
  44. '   XD,YD are the sizes, in pixels, of each square on the board. 
  45. Dim _BOARD(2,2)
  46. Global _BOARD(),WIN
  47. Global XD,YD
  48.  
  49. _INITIALIZE
  50. _GUIDATA
  51. _SETUPALL
  52. _SETPORTS
  53. Do 
  54.    K=J Wait Message
  55.    While K
  56.       C=J Tag Data(_MESSLIST,1)
  57.       If C=Equ("IDCMP_CLOSEWINDOW")
  58.          'Bring up a requester before quiting 
  59.          If J Easy Request(_OXWIND,"Quit"," Are you ure you"+Chr$(10)+"want to quit?"," Quit | Cancel ",0)
  60.             _CLEANUP
  61.          End If 
  62.       Else If C=Equ("IDCMP_REFRESHWINDOW")
  63.          _DOREFRESH
  64.          
  65.       Else If C=Equ("IDCMP_NEWSIZE")
  66.          'If the user resizes the window, redraw the board in a new size.         
  67.          _DRAW_BOARD
  68.  
  69.       Else If C=Equ("IDCMP_MOUSEBUTTONS") and J Tag Data(_MESSLIST,2)=Equ("SELECTUP") and WIN=0
  70.          'The user has tried to make a move.
  71.          _HANDLE_MOVE
  72.  
  73.       Else If C=Equ("IDCMP_MENUPICK")
  74.          'Read the menus in the normal way. There's only one menu, so we
  75.          'only need to read the Item. 
  76.          C=J Tag Data(_MESSLIST,2)
  77.          I=J Read Item(C)
  78.  
  79.          If I=0
  80.             'New Gane: Clear the board array, set the winner to 0, and 
  81.             'change the window title 
  82.             For Y=0 To 2
  83.                For X=0 To 2
  84.                   _BOARD(X,Y)=0
  85.                Next X
  86.             Next Y
  87.             _DRAW_BOARD
  88.             WIN=0
  89.             J Set Window Titles _OXWIND,_MOVE(_MYMOVE),_MOVE(2)
  90.  
  91.          Else If I=2
  92.             'Bring up a requester before quiting 
  93.             If J Easy Request(_OXWIND,"Quit"," Are you ure you"+Chr$(10)+"want to quit?"," Quit | Cancel ",0)
  94.                _CLEANUP
  95.             End If 
  96.  
  97.          End If 
  98.       End If 
  99.       K=J Next Message
  100.    Wend 
  101. Loop 
  102.  
  103. Procedure _DRAW_BOARD
  104.    On Error Proc _CLEANUP
  105.    
  106.    'First, get the inner size of the window, and split this into three
  107.    'parts 
  108.    J This Window _OXWIND
  109.    XE=J Window Width-J Border Right-1
  110.    YE=J Window Height-J Border Bottom-1
  111.    XD=(XE-J X Offset)/3 : YD=(YE-J Y Offset)/3
  112.    
  113.    'Clear the window - get rid of any rubbish that's there already. 
  114.    Ink 0
  115.    Bar J X Offset,J Y Offset To XE,YE
  116.  
  117.    'Draw the four lines across the window, according to the divisions 
  118.    'we worked out above.
  119.    Ink 1
  120.    For I=1 To 2
  121.       Draw J X Offset,J Y Offset+I*YD To XE,J Y Offset+I*YD
  122.       Draw J X Offset+I*XD,J Y Offset To J X Offset+I*XD,YE
  123.    Next I
  124.  
  125.    'Now fill in the counters. The _RENDER_COUNTER procedure does the
  126.    'actual drawing. 
  127.    For Y=0 To 2
  128.       For X=0 To 2
  129.          If _BOARD(X,Y)
  130.             _RENDER_COUNTER[X,Y,_BOARD(X,Y)]
  131.          End If 
  132.       Next X
  133.    Next Y
  134.  
  135. End Proc
  136. Procedure _RENDER_COUNTER[X,Y,T]
  137.    On Error Proc _CLEANUP
  138.    
  139.    'X,Y square to draw the counter  
  140.    'T type of counter 1=O 2=X 
  141.    Ink 2
  142.  
  143.    'This is the radius of a counter. Each counter therefore occupies
  144.    'two thirds of the square. Also find the centre of the square
  145.    RX=XD/3 : RY=YD/3
  146.    CX=(X+0.5)*XD+J X Offset : CY=(Y+0.5)*YD+J Y Offset
  147.    
  148.    'Draw in the appropriate counter from the above data.
  149.    If RX>0 and RY>0
  150.       If T=1
  151.          Ellipse CX,CY,RX,RY
  152.       Else 
  153.          Draw CX-RX,CY-RY To CX+RX,CY+RY
  154.          Draw CX+RX,CY-RY To CX-RX,CY+RY
  155.       End If 
  156.    End If 
  157.    
  158. End Proc
  159. Procedure _HANDLE_MOVE
  160.    On Error Proc _CLEANUP
  161.    
  162.    'Find the square the player wants to move to.
  163.    X=(J Tag Data(_MESSLIST,5)-J X Offset)/XD
  164.    Y=(J Tag Data(_MESSLIST,6)-J Y Offset)/YD
  165.  
  166.    'Check it's actually on the board, and that the square is empty
  167.    If X=>0 and X<=2 and Y=>0 and Y<=2
  168.       If _BOARD(X,Y)=0
  169.  
  170.          'place the counter in the BOARD array, and draw it. Change the move
  171.          'counter to the other players turn.
  172.          _BOARD(X,Y)=_MYMOVE+1
  173.          _RENDER_COUNTER[X,Y,_MYMOVE+1]
  174.          Add _MYMOVE,1,0 To 1
  175.  
  176.          'check for lines. If a line is found, WIN is set to the winning
  177.          'player, and the game is stopped until new game is selected. 
  178.          For I=0 To 2
  179.             If _BOARD(0,I)=_BOARD(1,I) and _BOARD(1,I)=_BOARD(2,I) and _BOARD(0,I)<>0
  180.                WIN=_BOARD(0,I)
  181.             End If 
  182.          Next I
  183.          For I=0 To 2
  184.             If _BOARD(I,0)=_BOARD(I,1) and _BOARD(I,1)=_BOARD(I,2) and _BOARD(I,0)<>0
  185.                WIN=_BOARD(I,0)
  186.             End If 
  187.          Next I
  188.          If _BOARD(0,0)=_BOARD(1,1) and _BOARD(1,1)=_BOARD(2,2) and _BOARD(0,0)<>0
  189.             WIN=_BOARD(0,0)
  190.          End If 
  191.          If _BOARD(2,0)=_BOARD(1,1) and _BOARD(1,1)=_BOARD(0,2) and _BOARD(2,0)<>0
  192.             WIN=_BOARD(2,0)
  193.          End If 
  194.  
  195.          'Change the window title to show whose turn or who won.
  196.          If WIN=0
  197.             J Set Window Titles _OXWIND,_MOVE(_MYMOVE),_MOVE(2)
  198.          Else 
  199.             J Set Window Titles _OXWIND,_MOVE(2+WIN),_MOVE(2)
  200.          End If 
  201.       End If 
  202.    End If 
  203.    
  204. End Proc
  205. 'procs below produced by Gadtools
  206.  
  207. Procedure _INITIALIZE
  208. Procedure _SETUPALL
  209. Procedure _GUIDATA
  210. Procedure _MAKEOXGADS
  211. Procedure _MAKEOXWIND[SC]
  212. Procedure _DOREFRESH
  213. Procedure _SETPORTS
  214. Procedure _FREEWIND[W,G,M,A,C]
  215. Procedure _CLEANUP
  216.